//
//  PictureShowOneViewController.m
//  CarHome
//
//  Created by lanou3g on 15-5-4.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "PictureShowOneViewController.h"
#import "UIImageView+WebCache.h"
#import "PicList.h"
#import "Picture.h"

@interface PictureShowOneViewController () <UICollectionViewDataSource, UICollectionViewDelegate, UIScrollViewDelegate>

@property (nonatomic, retain) UIView *rootView;
@property (nonatomic, retain) UICollectionView *collectionView;
@property (nonatomic, retain) UILabel *titleLabel;
@property (nonatomic, retain) UIView *headerView;
@property (nonatomic, retain) UIView *footerView;
@property (nonatomic, retain) UILabel *footerTitleLabel;
@property (nonatomic, retain) UIButton *returnBTN;
@property (nonatomic, retain) UIScrollView *oldScrollView;

@end

@implementation PictureShowOneViewController

- (void)loadView {
    self.rootView = [[[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds] autorelease];
    self.view = _rootView;
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout new] autorelease];
    flowLayout.itemSize = CGSizeMake(kWIDTH, kHEIGHT);
    flowLayout.minimumLineSpacing = 0;
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    self.collectionView = [[[UICollectionView alloc] initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:flowLayout] autorelease];
    [_rootView addSubview:_collectionView];
    
UIImageView *returnImage = [[[UIImageView alloc] initWithFrame:CGRectMake(-3, 0, 10, 20)] autorelease];
    returnImage.image = [UIImage imageNamed:@"bar_btn_icon_returntext_white"];
    self.returnBTN = [[[UIButton alloc] initWithFrame:CGRectMake(20, 10, 50, 20)] autorelease];
    [_returnBTN setTitle:@"返回" forState:UIControlStateNormal];
    _returnBTN.titleLabel.font = [UIFont boldSystemFontOfSize:17];
    [_returnBTN addSubview:returnImage];
    
    self.titleLabel = [[[UILabel alloc] initWithFrame:CGRectMake(kWIDTH / 2 - 10, 10, 50, 20)] autorelease];
    [_titleLabel setTextColor:[UIColor whiteColor]];
    _titleLabel.font = [UIFont boldSystemFontOfSize:16];
    UIImageView *image = [[[UIImageView alloc] initWithFrame:CGRectMake(kWIDTH - 40, 0, 40, 40)] autorelease];
    image.image = [UIImage imageNamed:@"bar_btn_icon_download_white"];
    
    UIImageView *image2 = [[[UIImageView alloc] initWithFrame:CGRectMake(kWIDTH - 80, 0, 40, 40)] autorelease];
    image2.image = [UIImage imageNamed:@"bar_btn_icon_share_white"];
    
    self.headerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 20, kWIDTH, 44)] autorelease];
    [_headerView addSubview:_returnBTN];
    [_headerView addSubview:_titleLabel];
    [_headerView addSubview:image];
    [_headerView addSubview:image2];
    
    self.footerTitleLabel = [[[UILabel alloc] initWithFrame:CGRectMake(20, 10, kWIDTH - 40, 20)] autorelease];
    _footerTitleLabel.font = [UIFont boldSystemFontOfSize:18];
    [_footerTitleLabel setTextColor:[UIColor whiteColor]];
    _footerTitleLabel.textAlignment = NSTextAlignmentCenter;
    self.footerView = [[[UIView alloc] initWithFrame:CGRectMake(0, kHEIGHT - 180, kWIDTH, 44)] autorelease];
    [_footerView addSubview:_footerTitleLabel];
    
    [_rootView addSubview:_footerView];
    [_rootView addSubview:_headerView];
    

}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationController.navigationBar setHidden:NO];
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    _collectionView.pagingEnabled = YES;
    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"PictureShowOneViewControllerIdentifier"];
    UITapGestureRecognizer *tpr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tprAction:)];
    UISwipeGestureRecognizer *tpr2 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(tprAction:)];
    [_collectionView addGestureRecognizer:tpr];
    [_collectionView addGestureRecognizer:tpr2];
    [_returnBTN addTarget:self action:@selector(returnBTNAction) forControlEvents:UIControlEventTouchUpInside];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _picList.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PictureShowOneViewControllerIdentifier" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor blackColor];
    
    PicList *pl = _picList[indexPath.row];
    NSString *bigPicURL = [pl.smallpic stringByReplacingOccurrencesOfString:@"s" withString:@"w"];
    _titleLabel.text = [NSString stringWithFormat:@"%d/%d", indexPath.row + 1, _picList.count];
    _footerTitleLabel.text = pl.specname;

    UIScrollView *scrollView = [[[UIScrollView alloc] initWithFrame:self.view.frame] autorelease];
    scrollView.userInteractionEnabled = YES;
    scrollView.contentSize = CGSizeMake(kWIDTH, kHEIGHT);
    scrollView.minimumZoomScale = 1;
    scrollView.maximumZoomScale = 3.0;
    scrollView.delegate = self;
    
    UIImageView *imageView = [[[UIImageView alloc] initWithFrame:CGRectMake((kWIDTH - kWIDTH) / 2, (kHEIGHT - (kWIDTH / 2 - 50)) / 2, kWIDTH, kHEIGHT / 2 - 50)] autorelease];
    [imageView sd_setImageWithURL:[NSURL URLWithString:bigPicURL] placeholderImage:nil];
    [scrollView addSubview:imageView];
//    NSLog(@"scrollview  x = %f , y = %f ", scrollView.center.x, scrollView.center.y);
//    NSLog(@"view  x = %f , y = %f ", imageView.center.x, imageView.center.y);
//    NSLog(@"view  x = %f , y = %f ", imageView.frame.origin.x, imageView.frame.origin.y);
    for (UIView *view in cell.subviews) {
        [view removeFromSuperview];
    }
    [cell addSubview:scrollView];
    
    return cell;
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    return scrollView.subviews[0];
}

/*

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    NSInteger row = _collectionView.contentOffset.x / kWIDTH;
    NSIndexPath *oldIndexPath = [NSIndexPath indexPathForRow:row inSection:0];
    self.oldScrollView  = [_collectionView cellForItemAtIndexPath:oldIndexPath].subviews[0];
    

}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    NSInteger row = _collectionView.contentOffset.x / kWIDTH;
    NSIndexPath *curIndexPath = [NSIndexPath indexPathForRow:row inSection:0];
    UIScrollView *curScrollView  = [_collectionView cellForItemAtIndexPath:curIndexPath].subviews[0];
//    if (curScrollView != _oldScrollView) {
//        for (UIScrollView *itemScrollView in _collectionView.visibleCells) {
//            <#statements#>
//        }
//        _oldScrollView.zoomScale = 1;
//        UIImageView *imageView = _oldScrollView.subviews[0];
//        imageView.frame = self.view.frame;
//    }
}
*/
 
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale {

    //获取到图片的frame
    CGRect viewFrame = view.frame;
    //判断内容视图的宽度大于scrollView本身宽度的时候 则为放大的情况
    if (scrollView.contentSize.width > scrollView.frame.size.width) {
        //放大时 图片的x应该为0
        viewFrame.origin.x = 0;
    } else {
        //缩小时，图片的x值的计算
        viewFrame.origin.x = (scrollView.frame.size.width - scrollView.contentSize.width) / 2;
    }
    
    if (scrollView.contentSize.height > scrollView.frame.size.height) {
        viewFrame.origin.y = 0;
    } else {
        viewFrame.origin.y = (scrollView.frame.size.height - scrollView.contentSize.height) / 2;
    }
    
    view.frame = viewFrame;

}



- (void)viewWillAppear:(BOOL)animated {
    CGPoint point = CGPointMake(kWIDTH * _row, kHEIGHT);
    _collectionView.contentOffset = point;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    [UIView animateWithDuration:.5 animations:^{
        _headerView.alpha = 0;
        _footerView.alpha = 0;
    }];
}

- (void)tprAction:(UIGestureRecognizer *)sender {
    if ([sender isKindOfClass:UITapGestureRecognizer.class]) {
        if (_headerView.alpha == 0) {
            [UIView animateWithDuration:.5 animations:^{
                _headerView.alpha = 1;
                _footerView.alpha = 1;
            }];
        } else {
            [UIView animateWithDuration:.5 animations:^{
                _headerView.alpha = 0;
                _footerView.alpha = 0;
            }];
        }
    }
}

- (void)returnBTNAction {
    [self dismissViewControllerAnimated:YES completion:nil];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
